<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of home
 *
 * @author b4by
 */
class home extends MY_Controller {

	private $limit_image = 12;

	function __construct() {
		parent::__construct();
		$this->data['is_home'] = true;
		$this->data['topUsers'] = $this->getTopUser('week');
	}

	public function index($page = 1) {
		$this->load->model('model_image');
		$pagination = pagination_init($this->model_image->get_total(array(
					'video' => 0,
					'suppend' => 0,
					'active' => 1
				)), $page, $this->limit_image);

		$images = $this->model_image->get_list(array(
			'where' => array(
				'video' => 0,
				'i.suppend' => 0,
				'i.active' => 1
			),
			'limit' => array($pagination->from, $pagination->limit),
			'order' => array('i.id', 'desc')
				));

		$this->data['method'] = __FUNCTION__;
		$this->data['new'] = 1;
		$this->data['title'] = lang('new_images');
		$this->data['images'] = $images;
		$this->data['temp'] = 'new/index';
		$this->data['pagination'] = $pagination;
		$this->loadView();
	}

	public function text($page = 1) {
		$this->load->model('model_image');
		$pagination = pagination_init($this->model_image->get_total(array(
					'video' => 0,
					'text_image' => 1,
					'suppend' => 0,
					'active' => 1
				)), $page, $this->limit_image);

		$images = $this->model_image->get_list(array(
			'where' => array(
				'video' => 0,
				'text_image' => 1,
				'i.suppend' => 0,
				'i.active' => 1
			),
			'limit' => array($pagination->from, $pagination->limit),
			'order' => array('i.id', 'desc')
				));

		$this->data['method'] = __FUNCTION__;
		$this->data['text_image'] = 1;
		$this->data['title'] = lang('text_images');
		$this->data['images'] = $images;
		$this->data['temp'] = 'new/index';
		$this->data['pagination'] = $pagination;
		$this->loadView();
	}

	public function video($page = 1) {
		$this->load->model('model_image');
		$pagination = pagination_init($this->model_image->get_total(array(
					'video' => 1,
					'suppend' => 0,
					'active' => 1
				)), $page, $this->limit_image);

		$images = $this->model_image->get_list(array(
			'where' => array(
				'video' => 1,
				'i.suppend' => 0,
				'i.active' => 1
			),
			'limit' => array($pagination->from, $pagination->limit),
			'order' => array('i.id', 'desc')
				));

		$this->data['method'] = __FUNCTION__;
		$this->data['new_video'] = 1;
		$this->data['title'] = lang('new_videos');
		$this->data['images'] = $images;
		$this->data['temp'] = 'new/index';
		$this->data['pagination'] = $pagination;
		$this->loadView();
	}

	public function hot($page = 1) {

		$this->load->model('model_image');
		$pagination = pagination_init($this->model_image->get_total(array(
					'active' => 1,
					'suppend' => 0,
				)), $page, $this->limit_image);
//		pr($pagination);die;
		$images = $this->model_image->get_list(array(
			'limit' => array($pagination->from, $pagination->limit),
			'order' => array('view_freq', 'desc'),
			'where' => array(
				'i.active' => 1,
				'suppend' => 0,
			),
				));

		$this->data['method'] = __FUNCTION__;
		$this->data['hot'] = 1;
		$this->data['title'] = lang('hot_images');
		$this->data['images'] = $images;
		$this->data['temp'] = 'new/index';
		$this->data['pagination'] = $pagination;
		$this->loadView();
	}

	public function old($page = 1) {
		$this->load->model('model_image');
		$pagination = pagination_init($this->model_image->get_total(array('new' => 0)), $page, $this->limit_image);

		$images = $this->model_image->get_list(array(
			'limit' => array($pagination->from, $pagination->limit),
			'where' => array('new' => 0),
				));

		$this->data['method'] = __FUNCTION__;
		$this->data['old'] = 1;
		$this->data['title'] = lang('old_images');
		$this->data['images'] = $images;
		$this->data['temp'] = 'new/index';
		$this->data['pagination'] = $pagination;
		$this->loadView();
	}

	public function vote($page = 1) {
		$this->load->model('model_image');
		$pagination = pagination_init($this->model_image->get_total(
						array(
							'new' => 1,
							'suppend' => 0,
							'active' => 0
						)
				), $page);

		$images = $this->model_image->get_list(array(
			'where' => array(
				'new' => 1,
				'i.suppend' => 0,
				'i.active' => 0
			),
			'limit' => array($pagination->from, $pagination->limit),
			'order' => array('i.id', 'desc')
				));
		$voted = $this->session->userdata('voted');
		foreach ($images as &$image) {
			if (isset($voted[$image->id])) {
				if ($voted[$image->id] === TRUE)
					$image->upVoted = 1;
				else
					$image->downVoted = 1;
			}
		}

		$this->data['method'] = __FUNCTION__;
		$this->data['pagination'] = $pagination;
		$this->data['vote'] = 1;
		$this->data['images'] = $images;
		$this->data['temp'] = 'vote/index';
		$this->loadView();
	}

}

?>
